<template>
  <div class="article-detail">
    <div v-if="loading" class="text-center">
      <div class="spinner-border" role="status">
        <span class="visually-hidden">加载中...</span>
      </div>
    </div>
    
    <div v-else-if="error" class="alert alert-danger">
      {{ error }}
    </div>
    
    <div v-else-if="article">
      <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><router-link to="/">首页</router-link></li>
          <li class="breadcrumb-item">
            <router-link :to="'/category/' + article.categoryId">{{ article.categoryName }}</router-link>
          </li>
          <li class="breadcrumb-item active">{{ article.title }}</li>
        </ol>
      </nav>
      
      <article class="card">
        <div class="card-body">
          <h1 class="card-title">{{ article.title }}</h1>
          <div class="article-meta mb-3">
            <span class="me-3">
              <router-link :to="'/category/' + article.categoryId" class="text-decoration-none">
                {{ article.categoryName }}
              </router-link>
            </span>
            <span class="me-3">{{ article.createdAt }}</span>
            <span class="me-3">浏览: {{ article.viewCount }}</span>
          </div>
          <div class="article-content">
            <p>{{ article.content }}</p>
          </div>
        </div>
      </article>
      
      <div class="mt-4">
        <router-link to="/" class="btn btn-outline-secondary">返回首页</router-link>
      </div>
    </div>
    
    <div v-else class="alert alert-warning">
      文章不存在或已被删除
    </div>
  </div>
</template>

<script>
import { computed, onMounted, watch } from 'vue'
import { useStore } from 'vuex'

export default {
  name: 'ArticleDetail',
  props: {
    id: {
      type: String,
      required: true
    }
  },
  setup(props) {
    const store = useStore()
    
    const article = computed(() => store.state.currentArticle)
    const loading = computed(() => store.state.loading)
    const error = computed(() => store.state.error)
    
    const fetchArticle = async () => {
      await store.dispatch('fetchArticleDetail', props.id)
    }
    
    onMounted(() => {
      fetchArticle()
    })
    
    watch(() => props.id, () => {
      fetchArticle()
    })
    
    return {
      article,
      loading,
      error
    }
  }
}
</script>

<style scoped>
.article-meta {
  color: #6c757d;
  font-size: 0.9rem;
}

.article-content {
  line-height: 1.6;
  white-space: pre-line;
}

.breadcrumb {
  background-color: transparent;
  padding: 0.75rem 0;
}
</style>